<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2019/2/26
 * Time: 21:36
 */

namespace app\admin\controller;


use app\common\consts\BaseConst;
use app\common\model\ProductData;
use app\common\service\AdminService;
use app\common\service\ChannelService;
use app\common\service\ProductService;
use app\common\service\UserService;
use app\common\utils\CommonUtils;

class Data extends Auth
{
    /**
     * 获取数据列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $productId = $this->request->get('productId');
        $beginTime = $this->request->get('beginTime');
        $endTime = $this->request->get('endTime');
        $userId = $this->request->get('userId');
        $channelId = $this->request->get('channelId', -1);
        $this->assign('productId', $productId);
        $this->assign('beginTime', $beginTime);
        $this->assign('endTime', $endTime);
        $this->assign('userId', $userId);
        $this->assign('channelId', $channelId);
        $map = [];
        if ($productId) {
            $map[] = ['product_id', '=', $productId];
        }
        if ($beginTime && $endTime) {
            $map[] = ['created', 'between', [strtotime($beginTime . ' 00:00:00'), strtotime($endTime . ' 23:59:59')]];
        }
        if ($userId) {
            $map[] = ['user_id', '=', $userId];
        }
        if ($channelId != -1) {
            $map[] = ['channel_id', '=', $channelId];
        }
        $productDataModel = new ProductData();
        $list = $productDataModel->where($map)->order('created desc,id desc')->paginate(BaseConst::Admin_DEFAULT_PAGE_SIZE);
        $count = $productDataModel->where($map)->count();
        $page = $list->render();
        $data = $list->toArray()['data'];
        $productIds = array_column($data, 'product_id');
        $userIds = array_column($data, 'user_id');
        $channelIds = array_column($data, 'channel_id');
        $productList = ProductService::batchProductByIds($productIds);
        $userList = UserService::batchUserByIds($userIds);
        $channelList = ChannelService::batchChannelByIds($channelIds);
        $newList = [];
        foreach ($list as $v) {
            $newList[] = [
                'id' => $v['id'],
                'productName' => isset($productList[$v['product_id']]) ? $productList[$v['product_id']]['name'] : "",
                'mobile' => isset($userList[$v['user_id']]) ? $userList[$v['user_id']]['mobile'] : '',
                'time' => date('Y-m-d H:i:s', $v['created']),
                'channelName' => isset($channelList[$v['channel_id']]) ? $channelList[$v['channel_id']]['name'] : "自然流量",
            ];
        }
        $productMapping = ProductService::getProductMapping();
        $channelMapping = ChannelService::getChannelMapping();
        $this->assign('list', $newList);
        $this->assign('page', $page);
        $this->assign('productMapping', $productMapping);
        $this->assign('channelMapping', $channelMapping);
        $this->assign('count', $count);
        return $this->fetch('data_list');
    }

    /**
     * 获取渠道数据列表
     * @author liud
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function channelDataList()
    {
        $channelId = $this->admin->channel_id == 0 ? -1 : $this->admin->channel_id;
        $beginTime = $this->request->get('beginTime');
        $endTime = $this->request->get('endTime');
        $this->assign('beginTime', $beginTime);
        $this->assign('endTime', $endTime);
        $map = [];
        if ($beginTime && $endTime) {
            $map[] = ['created', 'between', [strtotime($beginTime . ' 00:00:00'), strtotime($endTime . ' 23:59:59')]];
        }
        $map[] = ['channel_id', '=', $channelId];
        $map[] = ['is_reduce', '=', 0];
        $userModel = new \app\common\model\User();
        $list = $userModel->where($map)->order('created desc,id desc')->paginate(BaseConst::Admin_DEFAULT_PAGE_SIZE);
        $count = $userModel->where($map)->count();
        $page = $list->render();
        $newList = [];
        foreach ($list as $v) {
            $newList[] = [
                'id' => $v['id'],
                'mobile' => CommonUtils::hideMobile4($v['mobile']),
                'time' => date('Y-m-d H:i:s', $v['created']),
            ];
        }
        $this->assign('list', $newList);
        $this->assign('page', $page);
        $this->assign('count', $count);
        return $this->fetch('channel_data_list');
    }
}